package edu.ucla.cs.typecast.net;

import java.io.Serializable;
import java.net.InetAddress;

/**
 * 
 * This class represents a network layer end point. Currently it only supports
 * IP v4 UDP end point.
 * 
 * @date Jul 25, 2007
 *
 */
public class EndPoint implements Serializable, TypeCastConstants {

	public static EndPoint DEFAULT_RMI_ENDPOINT = null; 
	public static EndPoint DEFAULT_EVENT_ENDPOINT = null; 
	public static EndPoint DEFAULT_DISCOVERY_EVENT_ENDPOINT = null; 	

	static  { 
		try { 
			DEFAULT_RMI_ENDPOINT = new EndPoint(InetAddress.getByName(DEFAULT_RMI_ADDRESS),DEFAULT_RMI_PORT); 
			DEFAULT_EVENT_ENDPOINT = new EndPoint(InetAddress.getByName(DEFAULT_EVENT_ADDRESS),DEFAULT_EVENT_PORT); 
			DEFAULT_DISCOVERY_EVENT_ENDPOINT = new EndPoint(InetAddress.getByName(DEFAULT_DISCOVERY__EVENT_ADDRESS),DEFAULT_DISCOVERY_EVENT_PORT); 			
		} catch(Throwable t) { 
			t.printStackTrace(); 
		}
	}
	
	private InetAddress addr; 
	private int port; 

	
	public EndPoint(InetAddress addr, int port) { 
		this.addr = addr; 
		this.port = port; 
	}
	
	public InetAddress getAddress() {
		return addr; 
	}
	
	public int getPort() { 
		return port; 
	}
	
	public int hashCode() { 
		return addr.hashCode() ^ port; 
	}
	
	public boolean equals(Object another) { 
		if (another instanceof EndPoint) { 
			EndPoint endPoint = (EndPoint)another; 
			if (addr.equals(endPoint.getAddress()) && port == endPoint.getPort()) { 
				return true; 
			}
		}
		return false; 
	}
	
	public String toString() { 
		return new String(addr.getHostAddress() + ":" + port); 
	}
}
